package com.moonshineproject.utils
{
	import flash.xml.XMLNode;
	
	public class TextUtil
	{
		// Find word boundary from the beginning of the line
		public static function wordBoundaryForward(line:String):int
		{
			return line.length - line.replace(/^(?:\s+|[^\s,(){}\[\]\-+*%\/="'~!&|<>?:;.]+\s*|[,(){}\[\]\-+*%\/="'~!&|<>?:;.]+\s*)/,"").length; 
		}
		
		// Find word boundary from the end of the line
		public static function wordBoundaryBackward(line:String):int
		{
			return line.length - line.replace(/(?:\s+|[^\s,(){}\[\]\-+*%\/="'~!&|<>?:;.]+\s*|[,(){}\[\]\-+*%\/="'~!&|<>?:;.]+\s*)$/,"").length; 
		}
		
		// Get amount of indentation on line
		public static function indentAmount(line:String):int
		{
			return line.length - line.replace(/^\t+/,"").length;
		}
		
		// Count digits in decimal number
		public static function digitCount(num:int):int
		{
			return Math.floor(Math.log(num)/Math.log(10))+1;
		}
		
		// Escape a string so it can be fed into a new RegExp
		public static function escapeRegex(str:String):String {
			return str.replace(/[\$\(\)\*\+\.\[\]\?\\\^\{\}\|]/g,"\\$&");
		}
		
		// Repeats a string N times
		public static function repeatStr(str:String, count:uint):String {
			return new Array(count+1).join(str);
		}
		
		// Pad a string to 'len' length with 'char' characters
		public static function padLeft(str:String, len:uint, char:String = "0"):String {
			return repeatStr(char, len - str.length) + str;
		}
		
		public static function htmlUnescape(str:String):String
		{
    		return new XML(str).firstChild.nodeValue;
		}
		
		public static function htmlEscape(str:String):String
		{
    		return new XMLNode( 3, str ).toString();
		}

	}
}